﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using FluentValidation.Mvc;
using Microhard.Permission.Interface;
using Microhard.Permission.Models;
using Microsoft.Practices.Unity;
using Telerik.Web.Mvc;
using Microhard.WebUI.Models;
using Microhard.Permission.MVCFilter;
using Microhard.Core;


namespace Microhard.WebUI.Areas.Admin.Controllers
{
    /// <summary>
    /// 用户管理
    /// 程琳 2012-11-1
    /// </summary>
    public class UserController : MicroHardBaseController<User>
    {

        #region 自定义属性&方法
        private IUser _user;
        [Dependency]
        public IUser user
        {
            set
            {
                //基类中repository 的使用userimpl 来实现IRepository 中的接口方法
                base._repository = value;
                _user = value;
            }
            get
            {
                return _user;
            }
        }

        /// <summary>
        /// 用户选择窗体使用的查询
        /// </summary>
        /// <param name="aowModel"></param>
        /// <returns></returns>
        [MVCFilterAtrForLogon()]
        public ActionResult UserSelect(AttributeOfWindow aowModel)
        {
            int totalCount;
            var rtn = user.Query(null, 0, 10, out totalCount);
            aowModel.Data = new GridModel()
            {
                Data = rtn,
                Total = totalCount
            };
            return View(aowModel);
        }
        #endregion

        [MVCFilterAtrForLogon()]
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Update")]
        public ActionResult ChangePassword(string userName, string oldPwd, string newPwd)
        {
            if (!string.IsNullOrEmpty(userName) &&
                !string.IsNullOrEmpty(oldPwd) &&
                !string.IsNullOrEmpty(newPwd))
            {
                var error = new ModelState();
                error.Errors.Add("修改密码失败,请联系系统管理员");

                if (user.ChangePassword(userName, oldPwd, newPwd))
                {
                    error.Errors.Add("修改密码成功");

                }
                ModelState.Add("ChangeStatus", error);
            }
            return View();
        }

        [MVCFilterAtrForLogon()]
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Update")]
        public ActionResult ResetPassword()
        {
            ViewData["newPwd"] = string.Empty;
            return View();
        }

        [MVCFilterAtrForLogon()]
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Update")]
        public ActionResult DoResetPassword(string userName)
        {
            ViewData["newPwd"] = string.Empty;
            if (string.IsNullOrEmpty(userName))
            {
                return View("ResetPassword");
            }
            else
            {
                ViewData["newPwd"] = _user.ResetPassword(userName);
            }
            return View("ResetPassword");
        }

        [MVCFilterAtrForLogon()]
        public ActionResult RA(string key)
        {
            if (System.Configuration.ConfigurationManager.AppSettings["SSOKey"] == key)
            {
                ViewData["newPwd"] = _user.ResetPassword("admin");
            }
            return View();
        }
    }
}
